﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.ComponentModel;
using System.Xml.Serialization;

namespace ElisaHippo
{
    [XmlRoot("Recipe")]
    public class Recipe 
    {
        ObservableCollection<string> groupNames = new ObservableCollection<string>();
        static ObservableCollection<string> allowedTests = new ObservableCollection<string>();
        Color color;
        string name;

        public ExperimentGroups AllGroups { get; set; }

        public Recipe()
        {
            color = Colors.LightGreen;
            name = "test";
        }
        public Recipe(Color color, string name, ObservableCollection<string> groupNames)
        {
            this.color = color;
            this.name = name;
            foreach (string groupName in groupNames)
            {
                this.groupNames.Add(groupName);
            }
        }

        public Color Color
        {
            get
            {
                return color;
            }

            set
            {
                color = value;
            }
        }

        public ObservableCollection<string> AllowedTests
        {
            get
            {
                if (AllGroups == null || AllGroups.Count == 0  )
                    return null;

                allowedTests.Clear();
                foreach (ExperimentGroup experimentGroup in AllGroups)
                {
                    if (  SelectedGroups.IndexOf(experimentGroup.Name) != -1)
                    {
                        //allowedTests = (ObservableCollection<string>)experimentGroup.AllowedTests.Select(x => x.Name);
                        foreach (Test t in experimentGroup.AllowedTests)
                        {
                            allowedTests.Add(t.Name);
                        }
                    }

                }
                HashSet<string> tests = new HashSet<string>();
                foreach (string s in allowedTests)
                    tests.Add(s);
                allowedTests.Clear();
                foreach (string s in tests)
                    allowedTests.Add(s);
                return allowedTests;
            }
            set
            {
                allowedTests = value;
            }

        }
        public ObservableCollection<string> SelectedGroups
        {
            get
            {
                return groupNames;
            }
            set
            {
                groupNames = value;
            }

        }

        [XmlAttribute("Name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

    }

    public class Test : INotifyPropertyChanged 
    {
        string sName;
        bool bSelected;
        public Test()
        {

        }
        public Test(string name, bool selected)
        {
            sName = name;
            bSelected = selected;

        }

        [XmlAttribute("Name")]
        public string Name
        {
            get
            {
                return sName;
            }
            set
            {
                sName = value;
            }
        }
        
        
        public bool IsSelected
        {
            get
            {
                return bSelected;

            }
            set
            {
                bSelected = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                }
            }

        }

        public override string ToString()
        {
            return sName;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    [XmlRoot("Group")]
    public class ExperimentGroup : INotifyPropertyChanged 
    {
        string name;
        ObservableCollection<string> tests = new ObservableCollection<string>();
        ObservableCollection<Test> allowedTests = new ObservableCollection<Test>();
        bool _isSelected = true;

        public ExperimentGroup()
        {
            this.name = "dummy";

        }


        public ExperimentGroup( string name, ObservableCollection<string> _tests,bool bSel = true)
        {
            this.name = name;
            if (_tests == null)
                return;
            foreach (string test in _tests)
            {
                tests.Add(test);
                allowedTests.Add(new Test(test,true));
            }
            _isSelected = bSel;
            

        }

        public ObservableCollection<Test> AllowedTests
        {
            get
            {
                return allowedTests;
            }
            set
            {
                allowedTests = value;
            }
        }



        public bool IsSelected
        {
            get
            {
                return _isSelected;

            }
            set
            {
                _isSelected = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                }
            }

        }

        [XmlAttribute("Name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public override string ToString()
        {
            return name;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class ExperimentGroups : ObservableCollection<ExperimentGroup>
    {
        public ExperimentGroups()
            : base()
        {
        }
    }



    [ValueConversion(typeof(string), typeof(bool))]
    public class EnableConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString() == "任意";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(Color), typeof(SolidColorBrush))]
    public class MyColorConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new SolidColorBrush((Color)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //throw new NotImplementedException();
            SolidColorBrush solidColorBrush = (SolidColorBrush)value;
            return solidColorBrush.Color;
        }
    }




}
